<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="8">
            <a-form-model-item label="注册类别" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <a-select
                show-search
                v-model="model.categoryId"
                placeholder="请选择注册类别"
                style="width: 100%"
                allowClear
                :disabled="formDisabled"
                :filterOption="filterOption"
                @change="handleCategoryChange"
              >
                <a-select-option v-for="d in categoryList" :key="d.id">
                  {{ d.registrationCategory }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="注册专业" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="professionalId">
              <a-select
                show-search
                allowClear
                :disabled="formDisabled"
                v-model="model.professionalId"
                placeholder="请选择注册专业"
                style="width: 100%"
                :filterOption="filterOption"
              >
                <a-select-option v-for="d in professionalList" :key="d.id">
                  {{ d.registeredProfessional }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="证书名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="certificateName">
              <!-- <a-input :disabled="formDisabled" v-model="model.certificateName" placeholder="请输入证书名称"></a-input> -->
              <j-dict-select-tag
                :disabled="formDisabled"
                v-model="model.certificateName"
                :show-search="true"
                dictCode="erp_certificate_name_management,certificate_name,id"
                placeholder="请选择证书名称"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="颁发机构" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="issueBody">
              <a-input :disabled="formDisabled" v-model="model.issueBody" placeholder="请输入颁发机构"></a-input>            
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="颁发日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="issueDate">
              <j-date
                placeholder="请选择颁发日期"
                v-model="model.issueDate"
                :show-time="false"
                date-format="YYYY-MM-DD"
                :disabled="formDisabled"
                style="width: 100%"
              />         
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="继续教育时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="educationDate">
              <j-date
                placeholder="请选择继续教育时间"
                v-model="model.educationDate"
                :show-time="false"
                date-format="YYYY-MM-DD"
                :disabled="formDisabled"
                style="width: 100%"
              />         
            </a-form-model-item>
          </a-col>
          <a-col :span="8">
            <a-form-model-item label="长期有效" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isAlways">
              <j-dict-select-tag @input="isAlwaysChangeValue" type="radio" dictCode="YES_OR_NO" :disabled="formDisabled" v-model="model.isAlways" placeholder="请选择是否长期有效" />
            </a-form-model-item>
          </a-col>
          <a-col :span="8" :hidden="model.isAlways==1">
            <a-form-model-item label="到期时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <j-date
                placeholder="请选择到期时间"
                v-model="model.endDate"
                :show-time="false"
                date-format="YYYY-MM-DD"
                :disabled="formDisabled"
                style="width: 100%"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="attachment">
              <j-upload :disabled="formDisabled" v-model="model.attachment" :showToast="false" fileFormat=".pdf, .PDF" isSingle></j-upload>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
  import { getAction } from '@/api/manage'
  export default {
    name: 'ErpCertificationChildForm',
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
        },
        labelCol: {
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 24 },
        },
        confirmLoading: false,
        validatorRules: {
          certificateName: [{ required: true, message: '请选择证书名称!', trigger: ['change', 'blur'] }],
          issueBody: [{ required: true, message: '请输入颁发机构!', trigger: 'blur' }],
          categoryId: [{ required: true, message: '请选择注册类别!', trigger: ['change', 'blur'] }],
          professionalId: [{ required: true, message: '请选择注册专业!', trigger: ['change', 'blur'] }],
          issueDate: [{ required: true, message: '请选择颁发日期!', trigger: ['change', 'blur'] }],
          isAlways: [{ required: true, message: '请选择是否长期有效!', trigger: ['change', 'blur'] }],
          endDate: [{ required: true, message: '请选择有效期!', trigger: ['change', 'blur'] }],
          attachment: [{ required: true, message: '请选择附件!', trigger: ['change', 'blur'] }],
        },
        url: {
          add: "/api/erp/act/erpActQuitConfig/add",
          edit: "/api/erp/act/erpActQuitConfig/edit",
          queryById: "/api/erp/act/erpActQuitConfig/queryById"
        },
        flag: false,
        categoryList: [],
        professionalList: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      async getCategoryData(){
        let res = await getAction('/api/erp/files/professional/erpProfessionalCategory/list', { pageNo: 1,pageSize: -1 })
        if (res.success) {
          this.categoryList = res.result.records || res.result
        } else {
          this.categoryList = [];
        }
      },
      handleCategoryChange(val){
        if(val){
          if(this.categoryList.filter(v => v.id==val).length&&this.categoryList.filter(v => v.id==val)[0].children){
            this.professionalList = this.categoryList.filter(v => v.id==val)[0].children
          } else {
            this.professionalList = []
          }
        } else {
          this.professionalList = []
        }
        this.$set(this.model,'professionalId',undefined)
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      isAlwaysChangeValue(val){
        if(val==0){
          this.$set(this.validatorRules,'endDate',[{ required: true, message: '请选择到期时间!', trigger: ['change', 'blur'] }])
        } else {
          this.$delete(this.validatorRules,'endDate')
          this.$set(this.model,'endDate',null)
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      async edit (record) {
        await this.getCategoryData()
        this.$refs.form.clearValidate();
        this.model = Object.assign({}, record);
        if(this.model.id){
          this.isAlwaysChangeValue(this.model.isAlways)
          if(this.categoryList.filter(v => v.id==this.model.categoryId).length&&this.categoryList.filter(v => v.id==this.model.categoryId)[0].children){
            this.professionalList = this.categoryList.filter(v => v.id==this.model.categoryId)[0].children
          } else {
            this.professionalList = []
          }
        } else {
          this.professionalList = []
        }
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.$emit('ok', this.model, this.flag);
          } 
        })
        this.$emit('unButtonDisable')
      },
    }
  }
</script>
